﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace MyDevelopTool
{
    public class ConfigHelper
    {
        private string defaultFilePath = AppDomain.CurrentDomain.BaseDirectory + "Model";
        private string configFileName = AppDomain.CurrentDomain.BaseDirectory + "config.xml";
        public XmlDocument Doc = new XmlDocument();

        public void InitConfig()
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("Config");
            doc.AppendChild(root);
            XmlElement nameSpace = doc.CreateElement("NameSpace");
            root.AppendChild(nameSpace);
            string[] strSpaces = new string[] 
            { 
                "using System;", 
                "using System.Collections.Generic;",
                "using System.Data;",
                "using System.Linq;",
                "using System.Text;",
                "using X.DAL.Provider.Mapping;"
            };
            foreach (string space in strSpaces)
            {
                XmlElement spa = doc.CreateElement("Space");
                spa.InnerText = space;
                nameSpace.AppendChild(spa);
            }
            XmlElement connection = doc.CreateElement("ConnectionStrings");
            root.AppendChild(connection);
            XmlElement connStr = doc.CreateElement("add");
            connStr.SetAttribute("name", "SDPDB");
            connStr.SetAttribute("connectionString", "Server=SDPTESTNEW;Data Source=SDPTESTNEW;User Id=PL;Password=PL;");
            connStr.SetAttribute("providerName", "System.Data.OracleClient");
            connection.AppendChild(connStr);
            XmlElement filePath = doc.CreateElement("FilePath");
            filePath.SetAttribute("Path", defaultFilePath);
            root.AppendChild(filePath);
            doc.Save(configFileName);
        }

        public void AddConnection(string connectionString, string providerName)
        {
            CheckConfig();
            XmlNode xn = Doc.SelectSingleNode("/Config/ConnectionStrings");
            XmlElement conn = Doc.CreateElement("add");
            string name = connectionString.Substring(connectionString.IndexOf("Data Source="));
            name = name.Substring(12);
            name = name.Substring(0, name.IndexOf(';'));
            conn.SetAttribute("name", name);
            conn.SetAttribute("connectionString", connectionString);
            conn.SetAttribute("providerName", providerName);
            bool flag = false;
            if (xn.ChildNodes.Count > 0)
            {
                foreach (XmlNode node in xn.ChildNodes)
                {
                    if (node.Attributes["connectionString"].Value==connectionString)
                    {
                        flag = true;
                        if (node != xn.FirstChild)
                        {
                            xn.RemoveChild(node);
                            xn.InsertBefore(conn, xn.FirstChild);
                        }
                    }
                }
                if (!flag) xn.InsertBefore(conn,xn.FirstChild);
            }
            else xn.AppendChild(conn);
            Doc.Save(configFileName);
        }

        public List<RpcServer> ReadServers()
        {
            CheckConfig();
            List<RpcServer> lst = new List<RpcServer>();
            XmlNodeList xnl = Doc.SelectNodes("/Config/Servers/add");
            if (xnl.Count > 0)
            {
                foreach (XmlNode node in xnl)
                {
                    RpcServer server = new RpcServer();
                    server.Name = node.Attributes["name"].Value;
                    server.Host = node.Attributes["host"].Value;
                    server.Port = Convert.ToInt32(node.Attributes["port"].Value);
                    lst.Add(server);
                }
            }
            return lst;
        }

        public List<string> ReadConnection()
        {
            CheckConfig();
            List<string> lst = new List<string>();
            XmlNodeList xnl = Doc.SelectNodes("/Config/ConnectionStrings/add");
            if (xnl.Count > 0)
            {
                foreach (XmlNode node in xnl)
                {
                    string conn = node.Attributes["connectionString"].Value;
                    conn += "|" + node.Attributes["providerName"].Value;
                    lst.Add(conn);
                } 
            }
            return lst;
        }

        public void AddNameSpaces(string[] nameSpaces)
        {
            CheckConfig();
            nameSpaces = nameSpaces.Distinct().ToArray();
            XmlNode xn = Doc.SelectSingleNode("/Config/NameSpace");
            xn.RemoveAll();
            foreach (string space in nameSpaces)
            {
                XmlElement node = Doc.CreateElement("Space");
                node.InnerText = space;
                xn.AppendChild(node);
            }
            Doc.Save(configFileName);
        }

        public List<string> ReadNameSpaces()
        {
            CheckConfig();
            List<string> lst = new List<string>();
            XmlNodeList xnl = Doc.SelectNodes("/Config/NameSpace/Space");
            if (xnl.Count > 0)
            {
                foreach (XmlNode node in xnl)
                {
                    string space = node.InnerText;
                    lst.Add(space);
                }
            }
            return lst;
        }

        public void UpdateFilePath(string path)
        {
            CheckConfig();
            XmlNode xn = Doc.SelectSingleNode("/Config/FilePath");
            xn.Attributes["Path"].Value = path;
            Doc.Save(configFileName);
        }

        public string ReadFilePath()
        {
            CheckConfig();
            XmlNode xn = Doc.SelectSingleNode("/Config/FilePath");
            string path = xn.Attributes["Path"].Value;
            return path;
        }

        public void CheckConfig()
        {
            if (!File.Exists(configFileName))
            {
                InitConfig();
            }
            Doc.Load(configFileName);
        }
    }

    public class RpcServer
    {
        public string Name { get; set; }
        public string Host { get; set; }
        public int Port { get; set; }
    }
}
